package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.Clock;
import com.entity.Leave;
import com.service.AttendanceService;
import com.service.ClockService;
import com.service.LeaveService;
import com.service.sys.DepartmentService;
import com.service.sys.UserVoService;
import com.vo.AttendanceQueryVo;
import com.vo.AttendanceVo;
import com.vo.ClockVo;
import com.vo.LeaveVo;
import com.vo.sys.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Resource
    private ClockService clockService;

    @Resource
    private LeaveService leaveService;

    @Resource
    private UserVoService userVoService;

    @Resource
    private DepartmentService departmentService;

    @Override
    public List<AttendanceVo> queryAttendance(AttendanceQueryVo attendanceQueryVo) {
        QueryWrapper<UserVo> userVoQueryWrapper = new QueryWrapper<>();
        userVoQueryWrapper.eq(
                !StringUtils.isEmpty(attendanceQueryVo.getDepartmentId()),
                "department_id", attendanceQueryVo.getDepartmentId()
        );
        //获取用户列表
        List<UserVo> userVoList = userVoService.list(userVoQueryWrapper);
        List<AttendanceVo> attendanceVoList = new ArrayList<>();
        //遍历用户列表去构造单个人的某个时间段内的考勤记录
        for (UserVo userVo : userVoList) {
            AttendanceVo attendanceVo = new AttendanceVo();
            Clock clock = new Clock();
//            clock.setDepartmentId(attendanceQueryVo.getDepartmentId());
            clock.setUserId(userVo.getId());
            clock.setCreateTime(attendanceQueryVo.getStartTime());
            clock.setUpdateTime(attendanceQueryVo.getEndTime());
            List<ClockVo> clockVoList = clockService.select(clock);
            attendanceVo.setClockVoList(clockVoList);
            //打卡次数
            attendanceVo.setClockNum(clockVoList.size());

            //遍历clockVoList获取迟到次数和早退次数
            clockVoList.forEach(clockVo -> {
                if ("签到".equals(clockVo.getClockType())) {
                    int a = 0;
                    if ("1".equals(clockVo.getLateType())) {

                        a++;
                    }
                    attendanceVo.setLateNum(a);
                } else if ("签退".equals(clockVo.getClockType())) {
                    int b = 0;
                    if ("1".equals(clockVo.getLeaveEarlyType())) {

                        b++;
                    }
                    attendanceVo.setLeaveEarlyNum(b);
                }
            });

            attendanceVo.getLateNum();
            attendanceVo.getLeaveEarlyNum();


            Leave leave = new Leave();
//            leave.setDepartmentId(attendanceQueryVo.getDepartmentId());
            leave.setUserId(userVo.getId());
            leave.setCreateTime(attendanceQueryVo.getStartTime());
            leave.setUpdateTime(attendanceQueryVo.getEndTime());
            List<LeaveVo> leaveVoList = leaveService.select(leave);
            attendanceVo.setLeaveVoList(leaveVoList);
            attendanceVo.setLeaveNum(leaveVoList.size());
            attendanceVo.setUserId(userVo.getId());
            attendanceVo.setUserVo(userVo);
            attendanceVo.setDepartmentVo(departmentService.getById(userVo.getDepartmentId()));
            attendanceVoList.add(attendanceVo);
        }

        return attendanceVoList;
    }
}
